﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace TinyCinema.Data
{
    public class DeskType : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public decimal AdditionalPrice { get; set; }
        public static ObservableCollection<DeskType> lstType { get; set; }        
        public DeskType(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.DeskType);
            var tbl = ds.Tables[Tables.DeskType];
            var Row = tbl.Select(DeskTypeTable.ID + "='" + ID + "'")[0];
            Name = Row[DeskTypeTable.Name] as string;
            AdditionalPrice = (decimal)Row[DeskTypeTable.AdditionalPrice];

            if (lstType == null)
            {
                lstType = new ObservableCollection<DeskType>();
                lstType = GetAllDeskType();
            }
            
        }
        public DeskType()
        {
            ID = IDGenerate(Tables.DeskType);
            Save();
            lstType = GetAllDeskType();
        }
        public static ObservableCollection<DeskType> GetAllDeskType()
        {
            ObservableCollection<DeskType> Result = new ObservableCollection<DeskType>();

            PutTableIntoDataSet(Tables.DeskType);
            var tbl = ds.Tables[Tables.DeskType];
            var Rows = tbl.Select();

            foreach (var Row in Rows)
                Result.Add(new DeskType(Row[DeskTypeTable.ID] as string));
            return Result;
        }
        public void Save()
        {
            var tbl = ds.Tables[Tables.DeskType];
            var Rows = tbl.Select(DeskTypeTable.ID + "='" + ID + "'");
            if (Rows.Length == 0)
            {
                ID = IDGenerate(Tables.DeskType);
                var Row = tbl.NewRow();
                Row[DeskTypeTable.ID] = ID;
                Row[DeskTypeTable.Name] = Name;
                Row[DeskTypeTable.AdditionalPrice] = AdditionalPrice;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Row = Rows[0]; Row[DeskTypeTable.Name] = Name;
                Row[DeskTypeTable.AdditionalPrice] = AdditionalPrice;
            }
            SaveCommand();
            //Cập nhật lstType
            lstType = GetAllDeskType();
        }
        public void Delete()
        {
            PutTableIntoDataSet(Tables.DeskType);
            var tbl = ds.Tables[Tables.DeskType];
            var Row = tbl.Select(DeskTypeTable.ID + "='" + ID + "'")[0];
            Row.Delete();
            SaveCommand();
        }
        void SaveCommand()
        {
            string sql = "Select * From " + Tables.DeskType;
            SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlConnection);
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
            try { sqlConnection.Open(); }
            catch { }
            adapter.Update(ds.Tables[Tables.DeskType]);
            sqlConnection.Close();
            ds.AcceptChanges();
        }
    }
}
